<template>
  <el-dialog
    :title="!dataForm.newId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地区id" prop="mapId">
      <el-input v-model="dataForm.mapId" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="新房价格" prop="newPrice">
      <el-input v-model="dataForm.newPrice" placeholder="新房价格"></el-input>
    </el-form-item>
    <el-form-item label="新房面积" prop="newArea">
      <el-input v-model="dataForm.newArea" placeholder="新房面积"></el-input>
    </el-form-item>
    <el-form-item label="新房户型(1层、2层、3层、4层)" prop="newRoom">
      <el-input v-model="dataForm.newRoom" placeholder="新房户型(1层、2层、3层、4层)"></el-input>
    </el-form-item>
    <el-form-item label="新房类型(1.住宅 2.别墅 3.商业 4.写字楼)" prop="newType">
      <el-input v-model="dataForm.newType" placeholder="新房类型(1.住宅 2.别墅 3.商业 4.写字楼)"></el-input>
    </el-form-item>
    <el-form-item label="新房状态(1.未开盘 2.在售 3.售罄)" prop="newStatus">
      <el-input v-model="dataForm.newStatus" placeholder="新房状态(1.未开盘 2.在售 3.售罄)"></el-input>
    </el-form-item>
    <el-form-item label="新房装修(1.带装修 2.毛坯房)" prop="newFinish">
      <el-input v-model="dataForm.newFinish" placeholder="新房装修(1.带装修 2.毛坯房)"></el-input>
    </el-form-item>
    <el-form-item label="新房特色(1.近地铁 2.环境好 3.低价)" prop="newFeat">
      <el-input v-model="dataForm.newFeat" placeholder="新房特色(1.近地铁 2.环境好 3.低价)"></el-input>
    </el-form-item>
    <el-form-item label="新房图片" prop="newImg">
      <el-input v-model="dataForm.newImg" placeholder="新房图片"></el-input>
    </el-form-item>
    <el-form-item label="新房简介" prop="newIntro">
      <el-input v-model="dataForm.newIntro" placeholder="新房简介"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          newId: 0,
          mapId: '',
          newPrice: '',
          newArea: '',
          newRoom: '',
          newType: '',
          newStatus: '',
          newFinish: '',
          newFeat: '',
          newImg: '',
          newIntro: ''
        },
        dataRule: {
          mapId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          newPrice: [
            { required: true, message: '新房价格不能为空', trigger: 'blur' }
          ],
          newArea: [
            { required: true, message: '新房面积不能为空', trigger: 'blur' }
          ],
          newRoom: [
            { required: true, message: '新房户型(1层、2层、3层、4层)不能为空', trigger: 'blur' }
          ],
          newType: [
            { required: true, message: '新房类型(1.住宅 2.别墅 3.商业 4.写字楼)不能为空', trigger: 'blur' }
          ],
          newStatus: [
            { required: true, message: '新房状态(1.未开盘 2.在售 3.售罄)不能为空', trigger: 'blur' }
          ],
          newFinish: [
            { required: true, message: '新房装修(1.带装修 2.毛坯房)不能为空', trigger: 'blur' }
          ],
          newFeat: [
            { required: true, message: '新房特色(1.近地铁 2.环境好 3.低价)不能为空', trigger: 'blur' }
          ],
          newImg: [
            { required: true, message: '新房图片不能为空', trigger: 'blur' }
          ],
          newIntro: [
            { required: true, message: '新房简介不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.newId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.newId) {
            this.$http({
              url: this.$http.adornUrl(`/house/newhouse/info/${this.dataForm.newId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mapId = data.newHouse.mapId
                this.dataForm.newPrice = data.newHouse.newPrice
                this.dataForm.newArea = data.newHouse.newArea
                this.dataForm.newRoom = data.newHouse.newRoom
                this.dataForm.newType = data.newHouse.newType
                this.dataForm.newStatus = data.newHouse.newStatus
                this.dataForm.newFinish = data.newHouse.newFinish
                this.dataForm.newFeat = data.newHouse.newFeat
                this.dataForm.newImg = data.newHouse.newImg
                this.dataForm.newIntro = data.newHouse.newIntro
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/newhouse/${!this.dataForm.newId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'newId': this.dataForm.newId || undefined,
                'mapId': this.dataForm.mapId,
                'newPrice': this.dataForm.newPrice,
                'newArea': this.dataForm.newArea,
                'newRoom': this.dataForm.newRoom,
                'newType': this.dataForm.newType,
                'newStatus': this.dataForm.newStatus,
                'newFinish': this.dataForm.newFinish,
                'newFeat': this.dataForm.newFeat,
                'newImg': this.dataForm.newImg,
                'newIntro': this.dataForm.newIntro
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
